package cn.edu.sdjzu.xg.controller;

import cn.edu.sdjzu.xg.domain.University;
import cn.edu.sdjzu.xg.exception.BysjException;
import cn.edu.sdjzu.xg.service.UniversityService;
import cn.edu.sdjzu.xg.util.Condition;
import cn.edu.sdjzu.xg.util.ControllerHelper;
import cn.edu.sdjzu.xg.util.JSONUtil;
import cn.edu.sdjzu.xg.util.Pagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/university.ctl")
public class UniversityReadCtl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        String id_str = request.getParameter("id");
        Integer id_int = Integer.parseInt(id_str);
        try {
            University university = UniversityService.getInstance().find(id_int);
            String university_json = JSON.toJSONString(university,
                    SerializerFeature.DisableCircularReferenceDetect);
            response.getWriter().println(university_json);
        } catch (BysjException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null) {
            message.put("message", "未收到请求信息！ ");
            response.getWriter().println(message);
            return;
        }
        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);

            Collection<University> universityList = UniversityService.getInstance().findAll(pagination, conditionList);
            String universities_json = JSON.toJSONString(universityList,
                    SerializerFeature.DisableCircularReferenceDetect);
            response.getWriter().println(universities_json);
        } catch (BysjException e) {
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

}
